package com.party.mobile.biz.copartner;


import com.party.common.utils.PartyCode;
import com.party.common.utils.StringUtils;
import com.party.core.exception.BusinessException;
import com.party.core.model.city.Area;
import com.party.core.model.copartner.CopartnerLevel;
import com.party.core.model.crowdfund.ProjectWithAuthor;
import com.party.core.model.distributor.DistributorCount;
import com.party.core.model.distributor.DistributorRelation;
import com.party.core.model.member.Industry;
import com.party.core.model.member.Member;
import com.party.core.model.system.MemberSysRole;
import com.party.core.model.system.RoleEnum;
import com.party.core.model.system.RoleType;
import com.party.core.model.system.SysRole;
import com.party.core.service.city.IAreaService;
import com.party.core.service.copartner.ICopartnerLevelService;
import com.party.core.service.crowdfund.IProjectService;
import com.party.core.service.distributor.IDistributorCountService;
import com.party.core.service.distributor.IDistributorRelationService;
import com.party.core.service.member.IIndustryService;
import com.party.core.service.member.IMemberService;
import com.party.core.service.system.IMemberSysRoleService;
import com.party.core.service.system.ISysRoleService;
import com.party.mobile.biz.member.MemberBizService;
import com.party.mobile.web.dto.copartner.input.CopartnerRegisterInput;
import com.party.mobile.web.dto.copartner.output.GetDateOutput;
import com.party.mobile.web.dto.copartner.output.ProjectCountOutput;
import com.party.mobile.web.dto.copartner.output.RepresentProjectListOutput;
import com.party.mobile.web.dto.login.output.MemberOutput;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Nullable;
import java.util.List;

/**
 * 联盟机构合作商角色业务接口
 * Created by hqh.
 *
 * @date 2018/10/12 0006
 * @time 15:57
 */

@Service
@Transactional
public class CopartnerRoleBizService {

    @Autowired
    private IMemberService memberService;

    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private IMemberSysRoleService memberSysRoleService;

    @Autowired
    private ICopartnerLevelService copartnerLevelService;


    /**
     * 给新老用户初始化角色和上下级关系
     *
     * @param member 会员
     * @param inviteCode 邀请码
     * @return boolean
     */
    public boolean initializingRole(Member member,String inviteCode){

        SysRole sysRole = new SysRole();
        sysRole.setCode(RoleEnum.CHANNEL_DISTRIBUTOR_ROLE.getCode());
        sysRole.setType(RoleType.SYSTEM_ROLE.getCode());
        SysRole copartner = sysRoleService.uniqueProperty(sysRole);


        //如果角色存在
        List<MemberSysRole> memberSysRoles = memberSysRoleService.findByMemberId(member.getId());
        if(memberSysRoles.size()>0){
            for (MemberSysRole m:memberSysRoles){
                MemberSysRole msrObj = m;
                List<CopartnerLevel>  memberLevel =  copartnerLevelService.getCopartnerLevelByMultiple(new CopartnerLevel(member.getId(),null,null,0));
                //是渠道分销商
                if(copartner.getId().equals(msrObj.getRoleId()) && memberLevel!=null){
                    //已分配
                    return false;
                }
                //是合作渠道
                SysRole s2obj= sysRoleService.get(msrObj.getRoleId());
                if(null!=s2obj && RoleEnum.COSPONSOR_ROLE.getCode().equals(s2obj.getCode())){
                    //已分配
                    return false;
                }

            }
        }


        String partnerId = "";
        String partnerIds = "";

        if(StringUtils.isNotBlank(inviteCode)){
            Member inviteMember = memberService.findByPhone(inviteCode, Member.MEMBER_PERSONAL);
            if(null!=inviteMember){
                List<CopartnerLevel>   partners =  copartnerLevelService.getCopartnerLevelByMultiple(new CopartnerLevel(inviteMember.getId(),null,null,null));
                CopartnerLevel partnerCop = partners.get(0);

                partnerId=inviteMember.getId();

                if(StringUtils.isNotBlank(partnerCop.getParentIds())){
                    partnerIds = partnerCop.getParentIds()+","+inviteMember.getId();
                }else{
                    partnerIds =inviteMember.getId();
                }

                //下级报名人数
                Integer lowerNum =partnerCop.getLowerNum()+1;
                partnerCop.setLowerNum(lowerNum);
                copartnerLevelService.update(partnerCop);
            }else{
                throw new BusinessException(PartyCode.INVITE_CODE_ERROR, "邀请码不存在");
            }
        }

        //设置上下级关系
        CopartnerLevel copartnerLevel = new CopartnerLevel(member.getId(),partnerId,partnerIds,0);
        copartnerLevelService.insert(copartnerLevel);

        //设置角色
        MemberSysRole msrObj_new = new MemberSysRole(member.getId(),copartner.getId());
        memberSysRoleService.insert(msrObj_new);

        return true;
    }
}
